package com.itheima.mall.web.pms;


import com.itheima.mall.dto.ProductCategoryDto;
import com.itheima.mall.pojo.pms.ProductCategory;
import com.itheima.mall.service.pms.ProductCategoryService;
import com.itheima.mall.vo.R;
import com.itheima.mall.vo.pms.ProductCategoryChildrenVo;
import com.itheima.mall.vo.pms.ProductCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productCategory")
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 添加商品分类
     * @param productCategoryDto
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody ProductCategoryDto productCategoryDto){
        productCategoryService.create(productCategoryDto);
        return R.succeed(null,"操作成功");
    }

    /**
     * 删除商品分类
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R deleteById(@PathVariable("id") Integer id){
        Boolean result = productCategoryService.deleteById(id);
        return result?R.succeed(null,"操作成功"):R.error("商品分类正在使用,不能删除");
    }
    //TODO:查询所有一级分类及子分类
    @GetMapping("list/withChildren")
    public R withChildren(){
        List<ProductCategoryChildrenVo> productCategoryChildrenVo=productCategoryService.withChildren();
        return R.succeed(productCategoryChildrenVo,null);
    }

    //分页查询商品分类
    @GetMapping("list/{parentId}")
    public R selectPaging(@PathVariable Integer parentId,
                          @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){
        ProductCategoryVo productCategoryVo=productCategoryService.selectPaging(parentId,pageNum,pageSize);
        return R.succeed(productCategoryVo,null);
    }

    //修改导航栏显示状态(和下面的修改状态一样)
    @PostMapping("update/navStatus")
    public R updateNavStatus(@RequestParam(value = "navStatus") Integer navStatus, @RequestParam(value = "ids") Long[] ids){
        productCategoryService.updateNavStatus(navStatus,ids);
        return R.succeed(null,"修改成功");
    }

    //修改显示状态
    @PostMapping("update/showStatus")
    public R updateshowStatus(@RequestParam(value = "showStatus") Integer showStatus, @RequestParam(value = "ids") Long[] ids){
        productCategoryService.updateshowStatus(showStatus,ids);
        return R.succeed(null,"修改成功");
    }

    //修改商品分类
    @PostMapping("update/{id}")
    public R updateProduct(@RequestBody ProductCategory productCategory,@PathVariable("id") Integer id){
        productCategoryService.updateProduct(productCategory,id);
        return R.succeed(null,null);
    }

    //根据id获取商品分类(不确定什么是商品分类概念),但是完成
    @GetMapping("{id}")
    public R getProduct(@PathVariable("id") Integer id){
        ProductCategory productCategory=productCategoryService.getProduct(id);
        return R.succeed(productCategory,"查询成功");
    }
}
